<?php
require 'getFriendsForUser.php';
require 'getLinksForUser.php';
require 'Frameworks/Objects/UserLinks.php';

/*
returns an array of two according arrays sorted in descending way
(relatedFriends[], relatedLinksOfFriends[[]])
*/


function suggestFriendRelatedToLink($user, $link){

    $appFriends = getFriendsForUser($user);
//     print_r($appFriends);
// return;
	$i = 0;
	//for every friend
	foreach ($appFriends as $friend){
		$intoThisLinks = getLinksForUser($friend);

		//j = 4 links with the top scores
		//linksScores[i][j][0] = the link score
		//linksScores[i][j][1] = the affiliated link
		$linksScores[$i][0][0] = 0;
		$linksScores[$i][0][1] = "";

		$linksScores[$i][1][0] = 0;
		$linksScores[$i][1][1] = "";

		$linksScores[$i][2][0] = 0;
		$linksScores[$i][2][1] = "";

		$linksScores[$i][3][0] = 0;
		$linksScores[$i][3][1] = "";
		

		//for every link the friend is "into this"
		foreach ($intoThisLinks as $friendLink){
		
			$scores[] = blackBox($link, $friendLink);
			$links[] = $friendLink;

			//insert the link and score in sorted array
// 			for( $j = 0; $j < 4; $j++ ){
// 
// 				if ( $score > $linksScores[$i][$j][0] ){
// 					for( $k = $j; $k < 4; $k++ ){
// 						$tempScore = $linksScores[$i][$k][0];
// 						$tempLink  = $linksScores[$i][$k][1];
// 						
// 						$linksScores[$i][$k][0] = $score;
// 						$linksScores[$i][$k][1] = $friendLink;
// 						
// 						$score = $tempScore;
// 						$friendLink = $tempLink;
// 					}
// 				}
// 			}
		
		}

		
		
// 		$relatedLinks = 0;
// 		$friendScores[$i] = 0;
// 		$minScore = 1;
// 		for( $j=0; $j<4; $j++ ){
// 			if ( $linksScores[$i][$j][0] >= 0.8){
// 				$friendScores[$i] += $linksScores[$i][$j][0];
// 				if ($linksScores[$i][$j][0] < $minScore)
// 					$minScore = $linksScores[$i][$j][0];
// 				$relatedLinks++;
// 			}
// 		}
// 		
// 		if ($relatedLinks == 4){
// 			$friendScores[$i] -= $minScore;
// 		}
// 		else if ($relatedLinks < 3)
// 			$friendScores[$i] = 0;
		
		arsort($scores);
// print_r($scores);

		$relFriend[$i] = new UserLinks();
		$relFriend[$i]->setUserID($friend);
		foreach ($scores as $key => $val){
			echo "LINK: ";
			print $links[$key];
			echo "<br>";
			echo "score: ";
			print $val;
			echo "<br><br>";
			$relFriend[$i]->addLink($links[$key]);
		}
// echo "<br><br>";
// 		print_r($relFriend[$i]);
// 		echo "<br><br>";

		$relatedLinks = 0;
		$finalScores[$i] = 0;
		$minScore = 1;
		$break = 0;
		foreach ($scores as $score){
			if ( $score >= 0.8){
				$finalScores[$i] += $score;
				if ($score < $minScore)
					$minScore = $score;
				$relatedLinks++;
			}

			$break++;
			if ($break == 4)
				break;
		}
		
		if ($relatedLinks == 4){
			$finalScores[$i] -= $minScore;
		}
		else if ($relatedLinks < 3)
			$finalScores[$i] = 0;






		$i++;
	
	}
/////////////////////////////////////////////////////////////////

// 	print $finalScores[0];
// echo "<br><br>";
// 	print_r($finalScores);
// 	echo "<br><br>";
	$returnArr = array();
	arsort($finalScores);
	$i = 0;
	foreach ($finalScores as $key => $finalScore){
		if ($finalScore >= 2.4){
			$returnArr[$i] = $relFriend[$key];
		}
		$i++;
	}

	return $returnArr;



// 	//find the friend with the max score
// 	$i = 0;
// 	$maxKey = 0;
// 	$maxScore = 0;
// 	foreach ($friendScores as $score){
// 		if ($score > $maxScore){
// 			$maxScore = $score;
// 			$maxKey = $i;
// 			$i++;
// 		}
// 	}
// 	
// 	if($maxScore >= 2.4){
// 		$linkCount = 0;
// 		for( $j=0; $j<4; $j++ ){
// 			if ($linksScores[$maxKey][$j][0] >= 0.8){
// 				$linksToReturn[$linkCount] = $linksScores[$maxKey][$j][1];
// 				$linkCount++;
// 			}
// 		}
// 		
// 
// 		$arrKeys = array_keys($appFriends);
// 		//echo "<br>score: $maxScore<br>";
// 		$chosenFriend = $appFriends[$arrKeys[$maxKey]];
// 		return array(($chosenFriend), array_unique($linksToReturn) );
// 	}
// 	
// 	return -1;
	
}

function blackBox($url1, $url2){

	return rand( 700 , 1000 )/1000;;
}

?> 
